from selenium import webdriver
import time

from selenium.common.exceptions import NoSuchElementException, ElementNotInteractableException

profile = {}
opt = webdriver.ChromeOptions()
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=opt)


# 初始化配置信息
def _init_profile(auth_profile):
    profile["username"] = auth_profile["username"]
    profile["password"] = auth_profile["password"]


def authenticate(auth_profile):
    # 初始化配置信息
    _init_profile(auth_profile)
    driver.get('http://aaa.ncu.edu.cn')
    try:
        driver.find_element_by_id('username').send_keys(profile["username"])
        driver.find_element_by_id('password').send_keys(profile["password"])
        driver.find_element_by_id('remember').click()
        driver.find_element_by_id('login').click()
    except (NoSuchElementException, ElementNotInteractableException):
        pass
    time.sleep(1)
    driver.close()
